package pers.zw.study.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
/**
 * Using "config" to obtain init parameters.
 */
public class ServletDemo1 extends HttpServlet {
    private ServletConfig config;

    @Override
    public void init(ServletConfig config) {
        this.config = config;
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html"); //设置html输出格式

        String paramVal = config.getInitParameter("name");
        response.getWriter().print(paramVal);

        response.getWriter().print("<hr/>");

        Enumeration<String> all = config.getInitParameterNames();

        while (all.hasMoreElements()) {
            String name = all.nextElement();
            String val = config.getInitParameter(name);
            response.getWriter().print(name + "=" + val + "<br/>");
        }
    }

    @Override
    public void destroy() {
        super.destroy();
    }
}
